//
//  IMShareView.m
//  Common
//
//  Created by shiqing guo on 2018/6/6.
//

#import "IMShareView.h"
#import "Macro.h"
#import "UIView+DDAddition.h"
#import "UIButton+HYYImagePosition.h"

#define backHeight (290.f + IPhoneX_FixHeight)
#define btnWidth (75.f)
#define shareHeight (240.f)
#define spaceX ((UI_SCREEN_WIDTH - btnWidth * 4) / 5)

@interface IMShareView () <UITextFieldDelegate>

@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UILabel *lblCancel;
@property (nonatomic, strong) UIView *shareView;
@property (nonatomic, strong) NSArray *shareName;
@property (nonatomic, strong) NSArray *shareImg;

@property (nonatomic, assign) BOOL canSaveImg; // 保存长图的选项

@end

@implementation IMShareView

- (instancetype)initWithFrame:(CGRect)frame andSavePicture:(BOOL)savePicture
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [[EPColor epColor_Gray] colorWithAlphaComponent:0.5];
        UITapGestureRecognizer *myTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapRemoveView)];
        [self addGestureRecognizer:myTap];
        self.canSaveImg = savePicture;
        [self addSubviews];
    }
    return self;
}

- (void)addSubviews
{
    [self addSubview:self.backView];
//    self.backView.bottom = UI_CONTENT_HEIGHT;
    
    WEAKSELF
    [UIView animateWithDuration:0.5f animations:^{
        weakSelf.backView.bottom = UI_CONTENT_HEIGHT;
    } completion:^(BOOL finished) {}];
}

#pragma mark - 懒加载
- (UIView *)backView
{
    if (!_backView) {
        _backView = [[UIView alloc] initWithFrame:CGRectMake(0, UI_SCREEN_HEIGHT, UI_SCREEN_WIDTH, backHeight)];
        _backView.backgroundColor = [EPColor epColor_Bg];
        [_backView addSubview:self.shareView];
        [_backView addSubview:self.lblCancel];
    }
    return _backView;
}

- (UIView *)shareView
{
    if (!_shareView) {
        _shareView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, shareHeight)];
        NSArray *shareName = self.shareName;
        NSArray *shareImg = self.shareImg;
        for (int i = 0; i < shareName.count; i++) {
            UIButton *btn = [self creatBtnWithName:shareName[i] andImg:shareImg[i] andTag:i];
            [_shareView addSubview:btn];
        }
    }
    return _shareView;
}

- (NSArray *)shareName
{
    if (!_shareName) {
        if (self.canSaveImg) {
            _shareName = @[@"WeChat", @"朋友圈", @"FaceBook", @"QQ", @"短信", NSLocalizedString(@"CopyLine", @"复制链接"), NSLocalizedString(@"SaveLongPicture", @"保存长图")];
        } else {
            _shareName = @[@"WeChat", @"朋友圈", @"FaceBook", @"QQ", @"短信", NSLocalizedString(@"CopyLine", @"复制链接")];
        }
    }
    return _shareName;
}

- (NSArray *)shareImg
{
    if (!_shareImg) {
        if (self.canSaveImg) {
            _shareImg = @[@"share_wc", @"share_circl", @"share_fb", @"share_qq", @"share_massage", @"share_copy", @"share_picture"];
        } else {
            _shareImg = @[@"share_wc", @"share_circl", @"share_fb", @"share_qq", @"share_massage", @"share_copy"];
        }
    }
    return _shareImg;
}

- (UILabel *)lblCancel
{
    if (!_lblCancel) {
        _lblCancel = [[UILabel alloc] initWithFrame:CGRectMake(0, shareHeight, UI_SCREEN_WIDTH, 50.f)];
        _lblCancel.backgroundColor = [UIColor whiteColor];
        _lblCancel.text = @"取消";
        _lblCancel.font = SystemFont(16.f);
        _lblCancel.textColor = [EPColor epColor_descMessageGray];
        _lblCancel.textAlignment = NSTextAlignmentCenter;
        UITapGestureRecognizer *lblTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRemoveView)];
        [_lblCancel addGestureRecognizer:lblTap];
    }
    return _lblCancel;
}

- (UIButton *)creatBtnWithName:(NSString *)name andImg:(NSString *)img andTag:(NSInteger)tag
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    int a = (int)(tag / 4);
    int b = (int)(tag % 4);
    CGFloat x = (spaceX + btnWidth) * b + spaceX;
    CGFloat y = (100.f + 10.f) * a + 20.f;
    btn.frame = CGRectMake(x, y, btnWidth, 100.f);
    [btn setBackgroundColor:[UIColor clearColor]];
    [btn setTitleColor:[EPColor epColor_descMessageGray] forState:UIControlStateNormal];
    btn.titleLabel.font = SystemFont(14.f);
    [btn setImage:[UIImage imageNamed:img] forState:UIControlStateNormal];
    [btn setTitle:name forState:UIControlStateNormal];
    [btn setImagePosition:HYYImagePositionTop spacing:10.f];
    btn.tag = tag;
    [btn addTarget:self action:@selector(clickShareAction:) forControlEvents:UIControlEventTouchUpInside];
    
    return btn;
}

- (void)clickShareAction:(UIButton *)sender {
    NSInteger btnIndex = sender.tag;
    if (self.returnIndex){
        self.returnIndex((int)btnIndex);
        [self removeFromSuperview];
    }
}

- (void)tapRemoveView {
    if (self.tapBlock) self.tapBlock();
    [self removeFromSuperview];
}

@end
